import { Injectable } from '@nestjs/common';
import { DictPageRequest } from './dto/dict.request';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { SysDict } from './entities/dict.entity';
import { PageResult } from '../../common/result/PageResult';
import { SysDictType } from './entities/dict.type.entity';
import { Result } from '../../common/result/Result';
import { DictTypeForm } from './dto/dict.type.form';
import { StatusEnum } from '../../common/enums/StatusEnum';
import { DictForm } from './dto/dict.form';

@Injectable()
export class DictService {
  constructor(
    @InjectRepository(SysDict)
    private sysDictRepository: Repository<SysDict>,
    @InjectRepository(SysDictType)
    private sysDictTypeRepository: Repository<SysDictType>,
  ) {}
  async page(query: DictPageRequest) {
    // 1. 查询出所有的菜单
    const queryBuilder = await this.sysDictRepository
      .createQueryBuilder('su')
      .skip((query.pageNum - 1) * query.pageSize)
      .take(query.pageSize);
    if (query.name) {
      queryBuilder.andWhere('(su.name LIKE :name)', {
        name: `%${query.name}%`,
      });
    }
    if (query.typeCode) {
      queryBuilder.andWhere('(su.type_code = :typeCode)', {
        typeCode: query.typeCode,
      });
    }
    const result = await queryBuilder.getManyAndCount();
    return PageResult.success(result[0], result[1]);
  }

  async typesPage(query: DictPageRequest) {
    // 1. 查询出所有的菜单
    const queryBuilder = await this.sysDictTypeRepository
      .createQueryBuilder('su')
      .skip((query.pageNum - 1) * query.pageSize)
      .take(query.pageSize);
    if (query.keywords) {
      queryBuilder.andWhere('(su.name LIKE :keywords)', {
        keywords: `%${query.keywords}%`,
      });
    }
    const result = await queryBuilder.getManyAndCount();
    return PageResult.success(result[0], result[1]);
  }

  async typesForm(id: number) {
    const form = await this.sysDictTypeRepository.findOneBy({ id: id });
    return Result.success(form);
  }

  async addDictType(form: DictTypeForm) {
    const dictType = new SysDictType({});
    dictType.name = form.name;
    dictType.remark = form.remark;
    dictType.status = form.status;
    dictType.code = form.code;
    await this.sysDictTypeRepository.save(dictType);
    return Result.success();
  }

  async updateDictType(id: number, form: DictTypeForm) {
    const dictType = new SysDictType({});
    dictType.name = form.name;
    dictType.remark = form.remark;
    dictType.status = form.status;
    dictType.code = form.code;
    await this.sysDictTypeRepository.update({ id: id }, dictType);
    return Result.success();
  }

  async deleteDictType(ids: string) {
    const idList = ids.split(',');
    for (const id of idList) {
      await this.sysDictTypeRepository.softDelete({ id: Number(id) });
      return Result.success();
    }
  }

  async dictOptions(typeCode: string) {
    // 1. 获取所有的内容
    const dictList = await this.sysDictRepository.findBy({
      status: StatusEnum.ENABLE.value,
      typeCode: typeCode,
    });
    const result = [];
    dictList.forEach((dict) => {
      result.push({
        label: dict.name,
        value: dict.value,
        effectiveStartTime: dict.effectiveStartTime,
        effectiveEndTime: dict.effectiveEndTime,
      });
    });
    return Result.success(result);
  }

  async getDictForm(id: number) {
    const result = await this.sysDictRepository.findOneBy({ id: id });
    return Result.success(result);
  }

  async addDict(form: DictForm) {
    const dict = new SysDict();
    dict.name = form.name;
    dict.remark = form.remark;
    dict.sort = form.sort;
    dict.status = form.status;
    dict.typeCode = form.typeCode;
    dict.value = form.value;
    dict.effectiveStartTime = form.effectiveStartTime;
    dict.effectiveEndTime = form.effectiveEndTime;
    await this.sysDictRepository.save(dict);
    return Result.success(dict.id);
  }

  async updateDict(id: number, form: DictForm) {
    const dict = new SysDict();
    dict.name = form.name;
    dict.remark = form.remark;
    dict.sort = form.sort;
    dict.status = form.status;
    dict.typeCode = form.typeCode;
    dict.value = form.value;
    dict.effectiveStartTime = form.effectiveStartTime;
    dict.effectiveEndTime = form.effectiveEndTime;
    await this.sysDictRepository.update({ id: id }, dict);
    return Result.success(dict.id);
  }

  async deleteDict(ids: string) {
    const idList = ids.split(',');
    for (const id of idList) {
      await this.sysDictRepository.delete({ id: Number(id) });
    }
    return Result.success();
  }
}
